package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.CompanyEmployee;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.CompanyEmployeeService;
import com.xuanzheng.company.service.impl.CompanyEmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/submitJoinCompany")
public class SubmitJoinCompanyServlet extends HttpServlet {
    private final CompanyEmployeeService companyEmployeeService = new CompanyEmployeeServiceImpl();
    private final Gson gson = new Gson();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取当前用户
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("user");
            
            if (user == null) {
                result.put("success", false);
                result.put("message", "用户未登录");
                out.write(gson.toJson(result));
                return;
            }
            
            // 检查用户是否是普通用户
            if (user.getRole() != 0) {
                result.put("success", false);
                result.put("message", "只有普通用户可以申请加入公司");
                out.write(gson.toJson(result));
                return;
            }
            
            // 获取请求参数
            String companyId = request.getParameter("companyId");
            String reason = request.getParameter("reason");
            
            if (companyId == null || companyId.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "公司ID不能为空");
                out.write(gson.toJson(result));
                return;
            }
            
            // 创建入职申请对象
            CompanyEmployee application = new CompanyEmployee();
            application.setUserId(user.getId());
            application.setCompanyId(companyId);
            application.setReason(reason);
            
            // 提交申请
            boolean success = companyEmployeeService.submitApplication(application);
            
            if (success) {
                result.put("success", true);
                result.put("message", "申请提交成功，请等待公司负责人审核");
            } else {
                result.put("success", false);
                result.put("message", "申请提交失败，申请的该公司可能不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "申请提交失败：" + e.getMessage());
        }
        
        out.write(gson.toJson(result));
        out.flush();
    }
} 